package com.haizhi.crm.api.controller.visit;

import com.haizhi.crm.biz.model.VisitRecordQo;
import com.haizhi.crm.biz.model.VisitRecordSearchQo;
import com.haizhi.crm.biz.service.CompanyService;
import com.haizhi.crm.biz.service.EventService;
import com.haizhi.crm.biz.service.ProductService;
import com.haizhi.crm.biz.service.VisitRecordService;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.sys.auth.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Desc:拜访记录
 * Created by yangyijun on 2018/6/4.
 */
@Api(description = "[业务-拜访记录]" )
@RestController
@RequestMapping("/visitRecord" )
public class VisitRecordController {

    @Autowired
    private VisitRecordService visitRecordService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private EventService eventServcie;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "保存拜访记录", notes = "保存到mysql的visit_record表,同时根据图平台配置存入hbase、ES、GraphDB" )
    @PostMapping(value = "/save" )
    public Response save(@RequestBody @Valid VisitRecordQo visitRecordQo) {
        return visitRecordService.save(visitRecordQo);
    }

    @ApiOperation(value = "修改拜访记录", notes = "保存到mysql的visit_record表,同时根据图平台配置存入hbase、ES、GraphDB" )
    @PostMapping(value = "/update" )
    public Response update(@RequestBody @Valid VisitRecordQo visitRecordQo) {
        return visitRecordService.update(visitRecordQo);
    }

    @ApiOperation(value = "创建拜访日程-根据客户名查询客户", notes = "?customerName=北京" )
    @GetMapping("/findCustomersByName" )
    public Response findCustomersByName(@RequestParam String customerName) {
        return this.companyService.findByName(customerName);
    }

    @ApiOperation(value = "创建拜访日程-根据用户名字查询参与人", notes = "?name=李连杰" )
    @GetMapping("/findUsersByName" )
    public Response findUsersByName(@RequestParam String name) {
        return userService.findByName(name);
    }

    @ApiOperation(value = "创建拜访日程-展开填写更多信息-行内动态|行外事件-根据标题查询列表", notes = "eventSchema参数值：tv_inner_event（行内动态,name=账户转入）| tv_outer_event(行外事件,name=北京)" )
    @GetMapping("/findEventsByName" )
    public Response findEventsByName(@RequestParam String name, @RequestParam String eventSchema) {
        return eventServcie.findEventsByName(name, eventSchema);
    }

    @ApiOperation(value = "创建拜访日程-展开填写更多信息-根据产品名查询产品列表", notes = "?productName=活期" )
    @GetMapping("/findProductsByName" )
    public Response findProductsByName(@RequestParam String productName) {
        return productService.findByName(productName);
    }

    @ApiOperation(value = "创建拜访日程-展开填写更多信息-根据主题查询关联拜访日程", notes = "?title=产品" )
    @GetMapping("/findByTitle" )
    public Response findByTitle(@RequestParam String title) {
        return visitRecordService.findByTitle(title);
    }

    @ApiOperation(value = "拜访记录列表-获取创建者数据下拉框" )
    @GetMapping("/findCreators" )
    public Response findCreators() {
        return visitRecordService.findCreators();
    }

    @ApiOperation(value = "拜访记录列表-获取客户数据下拉框", notes = "queryType取值：my(我的)；underling（我下属的）" )
    @GetMapping("/findCustomers" )
    public Response findCustomers(@RequestParam String queryType) {
        return visitRecordService.findCustomers(queryType);
    }

    @ApiOperation(value = "拜访记录列表-条件查询拜访记录，条件：查询类型 | 时间范围 | 拜访公司key | 拜访主题 |", notes = "{\"pageNo\": 1, \"pageSize\": 10, \"queryType\": \"my\" };" )
    @PostMapping("/search" )
    public Response search(@RequestBody @Valid VisitRecordSearchQo visitRecordQo) {
        return visitRecordService.search(visitRecordQo);
    }

    @ApiOperation(value = "拜访记录列表-最近浏览-查询用户最近浏览的拜访记录", notes = "查询mysql库history_view_recent表" )
    @GetMapping("/recentViewVisitRecord" )
    public Response recentViewVisitRecord(@RequestParam int count) {
        return visitRecordService.recentViewVisitRecord(count);
    }

    @ApiOperation(value = "拜访记录详情-根据ID查询拜访记录基本信息", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findById" )
    public Response findById(@RequestParam String objectKey) {
        return visitRecordService.findById(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询相关联的产品", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findAssociatedProducts" )
    public Response findAssociatedProducts(@RequestParam String objectKey) {
        return visitRecordService.findAssociatedProducts(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询相关联的行内动态", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findAssociatedInnerEvents" )
    public Response findAssociatedInnerEvents(@RequestParam String objectKey) {
        return visitRecordService.findAssociatedInnerEvents(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询相关联的行外事件", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findAssociatedOuterEvents" )
    public Response findAssociatedOuterEvents(@RequestParam String objectKey) {
        return visitRecordService.findAssociatedOuterEvents(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询关联的拜访日程", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findAssociatedVisitRecords" )
    public Response findAssociatedVisitRecords(@RequestParam String objectKey) {
        return visitRecordService.findAssociatedVisitRecords(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询关联的客户", notes = "?objectKey=813E1FB1FB140A733B01945574D738ED" )
    @GetMapping("/findAssociatedCustomers" )
    public Response findAssociatedCustomers(@RequestParam String objectKey) {
        return visitRecordService.findAssociatedCustomers(objectKey);
    }

    @ApiOperation(value = "拜访记录详情-查询拜访记录同一公司的其他拜访日程", notes = "?visitRecordId=2 根据拜访记录的customer_key，查询mysql库invest_record表" )
    @GetMapping("/othersVisitRecordOfCompany" )
    public Response othersVisitRecordOfCompany(@RequestParam Long visitRecordId) {
        return visitRecordService.othersVisitRecordOfCompany(visitRecordId);
    }

    @ApiOperation(value = "拜访记录详情-根据ID删除拜访记录" )
    @GetMapping("/delete" )
    public Response delete(@RequestParam Long visitRecordId) {
        return visitRecordService.delete(visitRecordId);
    }
}
